package com.xinjie.www.dao.tieba;

import com.xinjie.www.dao.BaseDao;
import com.xinjie.www.po.Tieba;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TiebaDaoImpl implements TiebaDao{
    //获取贴吧列表
    @Override
    public List<Tieba> getAllTieba(Connection connection) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Tieba> tiebas = new ArrayList<>();
        String sql = "select * from tieba ";
        Object[] params = {};
        pstm = connection.prepareStatement(sql);
        rs = BaseDao.excute(connection,pstm,rs,sql,params);
        while (rs.next()){
            Tieba tieba = new Tieba();
            tieba.setTiebaId(rs.getInt("tieba_id"));
            tieba.setName(rs.getString("name"));
            tieba.setIntroduce(rs.getString("introduce"));
            tieba.setTime(rs.getString("time"));
            tieba.setManager(rs.getString("manager"));
            tieba.setPostNum(rs.getInt("post_num"));
            tiebas.add(tieba);
        }
        BaseDao.closeResource(null,pstm,rs);
        return tiebas;
    }

    //管理员查看自己所管理的贴吧
    @Override
    public List<Tieba> getOwnTieba(Connection connection, String manager) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Tieba> tiebas = new ArrayList<>();
        String sql = "select * from tieba where manager = ?";
        Object[] params = {manager};
        pstm = connection.prepareStatement(sql);
        rs = BaseDao.excute(connection,pstm,rs,sql,params);
        while (rs.next()){
            Tieba tieba = new Tieba();
            tieba.setTiebaId(rs.getInt("tieba_id"));
            tieba.setName(rs.getString("name"));
            tieba.setIntroduce(rs.getString("introduce"));
            tieba.setTime(rs.getString("time"));
            tieba.setManager(rs.getString("manager"));
            tieba.setPostNum(rs.getInt("post_num"));
            tiebas.add(tieba);
        }
        BaseDao.closeResource(null,pstm,rs);
        return tiebas;
    }
}
